package com.atguigui.leetcode;

import java.util.*;

/**
 * 1200.最小绝对差
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/7/4 9:12
 */
public class P1200MinimumAbsoluteDifference {
    public static void main(String[] args) {
        Solution solution = new P1200MinimumAbsoluteDifference().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        /**
         * 执行用时：69 ms, 在所有 Java 提交中击败了5.24%的用户
         * 内存消耗：73 MB, 在所有 Java 提交中击败了5.07%的用户
         *
         * @param arr
         * @return
         */
        public List<List<Integer>> minimumAbsDifference(int[] arr) {

            Map<Integer, List<List<Integer>>> map = new TreeMap<>();
            Arrays.sort(arr);

            for (int i = 1; i < arr.length; i++) {

                int abs = Math.abs(arr[i] - arr[i - 1]);
                if (map.containsKey(abs)) {
                    List<List<Integer>> list = map.get(abs);
                    ArrayList<Integer> arrayList = new ArrayList<>();
                    arrayList.add(arr[i - 1]);
                    arrayList.add(arr[i]);
                    list.add(arrayList);
                    map.put(abs, list);
                } else {
                    ArrayList<List<Integer>> list = new ArrayList<>();
                    ArrayList<Integer> arrayList = new ArrayList<>();
                    arrayList.add(arr[i - 1]);
                    arrayList.add(arr[i]);
                    list.add(arrayList);
                    map.put(abs, list);
                }
            }

            Integer next = map.keySet().iterator().next();

            return map.get(next);
        }
    }
}
